//
//  FoundViewController.m
//  TourPlay
//
//  Created by dllo on 15/11/16.
//  Copyright © 2015年 王军岐. All rights reserved.
//

#import "FoundViewController.h"
#import "DWFlowLayout.h"
#import "FoundCollectionViewCell.h"
#import "VideoController.h"
#import "VideoPlayerViewController.h"


@interface FoundViewController () <UICollectionViewDataSource, UICollectionViewDelegate, FoundCollectionViewDelegate>

@property (nonatomic, retain) UICollectionView *foundCollectionV;
@property (nonatomic, retain) NSMutableArray *videoArr;

@end

@implementation FoundViewController
- (void)dealloc
{
    [_foundCollectionV release];
    [_videoArr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.videoArr = [NSMutableArray array];

    self.navigationItem.title = @"今日精选";
     self.navigationItem.leftBarButtonItem = nil;
    

    DWFlowLayout *layout = [[DWFlowLayout alloc] init];
    // 设置每个item(cell)的大小
    layout.itemSize = CGSizeMake(SCREEN_WIDTH - 60, SCREEN_HEIGHT - 49 - 64 - 60);
//    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.foundCollectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 49 - 64) collectionViewLayout:layout];
    // 隐藏滑动条
    self.foundCollectionV.showsHorizontalScrollIndicator = NO;
    self.foundCollectionV.delegate = self;
    self.foundCollectionV.dataSource = self;
    [self.foundCollectionV registerClass:[FoundCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    self.foundCollectionV.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.foundCollectionV];
    [_foundCollectionV release];
    [layout release];
    
    // var1.0 砍掉
//    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"往期" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarAction)] autorelease];
    
//    self.navigationItem.rightBarButtonItem.tintColor = NAVG_FONTCOLOR_ON;
    

    [self getUpdate];
}
- (void)rightBarAction
{
    VideoController *videoC = [[VideoController alloc] init];
    

    videoC.tabBarController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:videoC animated:YES];
    [videoC release];
}
- (void)goToPlayer:(UIButton *)btn;
{
    FoundCollectionViewCell *cell = (FoundCollectionViewCell *)[btn.superview superview];
    NSIndexPath *indexPath = [self.foundCollectionV indexPathForCell:cell];
    
    VideoPlayerViewController *playC = [[VideoPlayerViewController alloc] init];
    
    playC.videoMod = [self.videoArr objectAtIndex:indexPath.row];
    [playC setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [self presentViewController:playC animated:YES completion:^{
        
    }];
    [playC release];

}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.videoArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellStr = @"cell";
    
    FoundCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellStr forIndexPath:indexPath];

    cell.delegate = self;
    cell.videoMod = [self.videoArr objectAtIndex:indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

#pragma mark - 获取数据
- (void)getUpdate
{
    // 让菊花旋转起来
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [hub show:YES];
    hub.labelText = @"努力加载中...";
    
    
    NSString* date = nil;
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"YYYYMMdd"];
    date = [formatter stringFromDate:[NSDate date]];
    [formatter release];
    NSLog(@"获取时间:%@", date);
    
    NSString *url = [NSString stringWithFormat:@"http://baobab.wandoujia.com/api/v1/feed?num=2&date=%@&vc=125&u=29724b3d1af7daa52090420444a8c8daa9a330c0", date];
    
    NSLog(@"%@", url);
    [AppTools getAFNet:url block:^(id result) {
        NSMutableDictionary *bigDic = result;
//        NSMutableArray *bigArr = [[[bigDic objectForKey:@"dailyList"] firstObject] objectForKey:@"videoList"];
        for (NSMutableDictionary *dicTemp in [bigDic objectForKey:@"dailyList"]) {
            for (NSMutableDictionary *dic in [dicTemp objectForKey:@"videoList"]) {
                VideoModel *videoMod = [VideoModel modelWithDictionary:dic];
                [self.videoArr addObject:videoMod];
            }
        }
        
        
        [self.foundCollectionV reloadData];
        [hub hide:YES];
    } errorBlock:^(NSError *error) {
        [self NetworkError:^{
        }];
        [hub hide:YES];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
